package com.kwok.openplatform.controller;

import com.github.pagehelper.PageInfo;
import com.kwok.openplatform.entity.Customer;
import com.kwok.openplatform.common.result.TableData;
import com.kwok.openplatform.service.CustomerService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户表(Customer)表控制层
 *
 * @author Kwok
 * @since 2024-07-21 18:31:38
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {

    Log log = LogFactory.getLog(CustomerController.class);

    @Autowired
    private CustomerService customerService;

    @GetMapping("/pageList")
    public TableData<Customer> pageList(Integer page, Integer limit,Customer customer){
        PageInfo<Customer> pageInfo = customerService.queryCustomers(page,limit,customer);
        log.info("分页数据："+pageInfo.getList());
        TableData<Customer> tableData = new TableData<>(0,"成功状态",pageInfo.getTotal(),pageInfo.getList());
        return tableData;
    }

    @GetMapping("/list")
    public List<Customer> list(){
        List<Customer> list = customerService.queryCustomerList();
        return list;
    }

    @PostMapping("/save")
    public String save(Customer customer){
        Integer integer = customerService.saveCustomer(customer);
        log.info("新增了"+integer+"条记录");
        return integer + "";
    }

    @PostMapping("/update")
    public String update(Customer customer){
        Integer integer = customerService.updateCustomerById(customer);
        log.info("修改了"+integer+"条记录");
        return integer + "";
    }

    @DeleteMapping("/remove/{id}")
    public String remove(@PathVariable Integer id){
        Integer integer = customerService.removeCustomerById(id);
        log.info("删除了"+integer+"条记录");
        return integer + "";
    }

    @PostMapping("/batchDelete")
    public String delete(Integer... ids){
        Integer integer = customerService.batchRemoveCustomerById(ids);
        log.info("批量删除了"+integer+"条记录");
        return integer + "";
    }

}

